package com.shanjiduo.newsline.getui;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;

import com.igexin.sdk.PushManager;
import com.igexin.sdk.Tag;

import java.io.File;

/**
 * Created by Administrator on 2017/1/12.
 */

public class PushUtils {
    public static final int REQUEST_PERMISSION = 0;
    public Activity activity;
    // DemoPushService.class 自定义服务名称, 核心服务
    private Class userPushService = DemoPushService.class;

    public PushUtils(Activity activity) {
        this.activity = activity;
        if (checkPermission()) {//权限检测，申请
            requestPermission();
        } else {
            initPush();//初始化注册服务
        }
        checkSO();
    }

    /**
     * 判断网络是否连接.
     */
    public boolean isNetworkConnected() {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        return mNetworkInfo != null && mNetworkInfo.isAvailable();
    }

    /**
     * 第二步：初始化推送服务，注册推送服务，服务正常运行的前提
     */
    public void initPush() {
        // PushManager.getInstance().initialize(this.getApplicationContext(), null);//使用标准个推 SDK Service
        PushManager.getInstance().initialize(activity.getApplicationContext(), userPushService);//使用自定义Service

        // 注册 intentService 后 PushDemoReceiver 无效, sdk 会使用 DemoIntentService 传递数据,
        // AndroidManifest 对应保留一个即可(如果注册 DemoIntentService, 可以去掉 PushDemoReceiver, 如果注册了
        // IntentService, 必须在 AndroidManifest 中声明)
        PushManager.getInstance().registerPushIntentService(activity.getApplicationContext(), DemoIntentService.class);
    }

    /**
     * 第三步：检测SO文件是否存在
     *
     * @return
     */
    public boolean checkSO() {
        File file = new File(activity.getApplicationInfo().nativeLibraryDir + File.separator + "libgetuiext2.so");
        return file.exists();
    }

    /**
     * 测试addTag接口.
     */
    public void addTag(Tag[] tags) {
        PushManager.getInstance().setTag(activity, tags, "" + System.currentTimeMillis());
    }

    /**
     * 获取个推SDK版本号
     */
    public void getVersion() {
        String version = PushManager.getInstance().getVersion(activity);
        Toast.makeText(activity, "当前sdk版本 = " + version, Toast.LENGTH_SHORT).show();
    }

    /**
     * 解除别名绑定
     */
    public void unBindAlias(String alias) {
        PushManager.getInstance().unBindAlias(activity, alias, false);
    }

    /**
     * 绑定别名
     */
    public void bindAlias(String alias) {
        PushManager.getInstance().bindAlias(activity, alias);
    }

    /**
     * 申请权限
     */
    public void requestPermission() {
        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE},
                REQUEST_PERMISSION);
    }


    /**
     * 第一步：Android 6.0版本 权限检测
     *
     * @return
     */
    public boolean checkPermission() {
        PackageManager pkgManager = activity.getPackageManager();
        // 读写 sd card 权限非常重要, android6.0默认禁止的, 建议初始化之前就弹窗让用户赋予该权限
        boolean sdCardWritePermission =
                pkgManager.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, activity.getPackageName()) == PackageManager.PERMISSION_GRANTED;
        // read phone state用于获取 imei 设备信息
        boolean phoneSatePermission =
                pkgManager.checkPermission(Manifest.permission.READ_PHONE_STATE, activity.getPackageName()) == PackageManager.PERMISSION_GRANTED;

        return Build.VERSION.SDK_INT >= 23 && !sdCardWritePermission || !phoneSatePermission;
    }

}
